package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.yalantis.ucrop.UCrop;
import com.youhong.teethcare.R;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.ImageUtils;
import com.youhong.teethcare.utils.ImageUtilss;

import java.io.File;
import java.util.Calendar;
import java.util.Random;

public class PortraitSelectActivity extends Activity implements View.OnClickListener {

    /**
     * Views
     **/

    TextView tv_portraitFromPhoto;
    TextView tv_portraitFromGallery;
    TextView tv_cancel;
    TextView tv_prompt;

    Uri portrait_uri ;


    Uri capture_uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
            + "temp_camera" + new Random(Calendar.getInstance().getTimeInMillis()).nextFloat() + ".png"));

    /**
     * Constants
     **/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portrait_select);
        portrait_uri= ImageUtils.getIconUri(this, Common.userInfo.getToken());
    }

    boolean isInitalized = false;

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        if (!isInitalized) {

            getViews();
            adjustViewsParamLayouts();
            isInitalized = !isInitalized;
        }
    }


    private void adjustViewsParamLayouts() {

        int screen_width = super.getWindowManager().getDefaultDisplay().getWidth();
        int screen_height = super.getWindowManager().getDefaultDisplay().getHeight();

        Window window = super.getWindow();
        WindowManager.LayoutParams window_layout = super.getWindow().getAttributes();

        window.setBackgroundDrawable(super.getResources().getDrawable(R.color.transparent));

        window_layout.width = (int) (screen_width * 0.9);
        window_layout.height = (int) (screen_height * 0.33);

        tv_prompt.getLayoutParams().height = (int) (screen_height * 0.07);
        tv_portraitFromPhoto.getLayoutParams().height = (int) (screen_height * 0.08);
        tv_portraitFromGallery.getLayoutParams().height = (int) (screen_height * 0.08);
        tv_cancel.getLayoutParams().height = (int) (screen_height * 0.08);
        window_layout.gravity = Gravity.BOTTOM;

        adjustFonts();
    }

    private void adjustFonts() {


        // this.tv_cancel.setTypeface(yahei_typeface);

    }

    private void getViews() {
        capture_uri=ImageUtils.createImagePathUri(this, "head");
        tv_cancel = (TextView) this.findViewById(R.id.portraitSelect_tv_cancel);
        tv_portraitFromPhoto = (TextView) this.findViewById(R.id.portraitSelect_tv_option1);
        tv_portraitFromGallery = (TextView) this.findViewById(R.id.portraitSelect_tv_option2);
        tv_prompt = (TextView) this.findViewById(R.id.portraitSelect_tv_prompt);
        tv_cancel.setOnClickListener(this);
        tv_portraitFromGallery.setOnClickListener(this);
        tv_portraitFromPhoto.setOnClickListener(this);


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {



        if (requestCode == ImageUtilss.GET_IMAGE_BY_CAMERA && resultCode == Activity.RESULT_OK) {


          //  ImageUtilss.cropImage(this, 300, 300, capture_uri, portrait_uri);
                cropImage( capture_uri, portrait_uri);
        } else if (requestCode == ImageUtilss.GET_IMAGE_FROM_PHONE && resultCode == Activity.RESULT_OK) {
            cropImage( data.getData(), portrait_uri);
          //  ImageUtilss.cropImage(this, 300, 300, data.getData(), portrait_uri);

        } else if (requestCode == UCrop.REQUEST_CROP&& resultCode == Activity.RESULT_OK) {

            Intent return_intent = new Intent();
            return_intent.setData(portrait_uri);
            this.setResult(Activity.RESULT_OK, return_intent);
            this.finish();
        }

    }
    public void cropImage(Uri srcUri, Uri targetUri) {
        UCrop.Options options = new UCrop.Options();
        options.setHideBottomControls(true);
        options.setCircleDimmedLayer(true);
        UCrop.of(srcUri, targetUri)
                .withOptions(options)
                .withAspectRatio(1, 1)
                .start(this);
    }
    @Override
    public void onClick(View v) {

        if (v == tv_portraitFromGallery) {

            ImageUtilss.openLocalImage(this);

        } else if (v == tv_portraitFromPhoto) {

            //capture_uri = ImageUtilss.getImageContentUri(this, capture_uri);
           // portrait_uri = ImageUtilss.getImageContentUri(this, portrait_uri);

            ImageUtilss.openCameraImage(this,
                    capture_uri);

        } else if (v == tv_cancel) {

            this.setResult(Activity.RESULT_CANCELED);
            this.finish();

        }

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.push_bottom_out);
    }
}
